{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}
<br>

<form class="layui-form" action="" id="cross_srh" lay-filter="cross_srh">
    <div class="layui-inline">
        <label class="layui-form-label">用户名：</label>
        <div class="layui-input-inline">
            <input type="text"style="width: 212px;" class="layui-input" name="userName" id="userName" placeholder="请输入">
        </div>
    </div>

    <div class="layui-inline">
        <label class="layui-form-label">密码：</label>
        <div class="layui-input-inline">
            <input type="text" style="width: 212px;" class="layui-input" name="passWord" id="passWord" placeholder="请输入">
        </div>
    </div>
    <br><br>
    <div class="layui-inline">
        <label class="layui-form-label">权限：</label>
        <div class="layui-input-inline">
            <select name="role" lay-filter="role" data-value="'+d.role+'">
                <option value="0">成员</option>
                <option value="1">管理员</option>
            </select>
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">项目：</label>
        <div class="layui-input-inline">
            <div  id="project_id" style="width: 212px;"></div>
        </div>
    </div>

    <div class="layui-inline" style="display:none">
        <label class="layui-form-label">密码：</label>
        <div class="layui-input-inline">
            <input type="text" style="width: 212px;" class="layui-input" name="id" id="id" placeholder="请输入">
        </div>
    </div>

    <div class="layui-input-inline">
        <button class="layui-btn" type="button" id="start" lay-submit lay-filter="start">保存</button>
    </div>
</form>

<script>
    layui.use(['table','code','xmSelect'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;
          var xmSelect = layui.xmSelect;
          UserInfoCache = layui.data('test').UserInfoCache;
          if(UserInfoCache!=undefined){
                form.val('cross_srh',{
                     userName:UserInfoCache.userName
                     ,role:UserInfoCache.role
                     ,project_id:UserInfoCache.project_id
                     ,id:UserInfoCache.id
                });
                project_id = UserInfoCache.project_id;
          }else{
              project_id = "";
          }
          form.render();

          $.ajax({
                                 //发出请求
                                type:"GET",
                                url:"/project/all/select",
                                contentType: 'application/json; charset=utf-8',
                                dataType:"json",
                                async:false,
                                success: function(result){
                                    var blSlt = false;
                                    var sarray = new Array();

                                    layui.each(result.data,function(k,v){
                                        if(project_id!=""){
                                            project_list = project_id.split(',');
                                            layui.each(project_list,function(i,data){
                                                if(data == v.id){
                                                    blSlt=true;
                                                }
                                            });
                                        }
                                        sarray.push({"name":v.project_name,"value":v.id,selected: blSlt});
                                    });

                                    var demo1 = xmSelect.render({
                                        el: '#project_id',
                                        data: sarray
                                    });
                                }
                            });

          //保存
          form.on('submit(start)', function(data){

                if(UserInfoCache!=undefined){
                    $.ajax({
                        //发出请求
                        type:"POST",
                        url:"/user/update",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:JSON.stringify(data.field),
                        success: function(result){
                            if(result.id){
                                layer.msg("保存成功!");
                            }
                        }
                    });
                    return false;
                }

                if($('#userName').val()!=""){
                    $.ajax({
                        //发出请求
                        type:"POST",
                        url:"/register",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:JSON.stringify(data.field),
                        success: function(result){
                            if(result.id){
                                layer.msg("保存成功!");
                            }else{
                                layer.msg("用户名已经存在!");
                            }
                        }
                    });
                }else{
                    layer.msg("用户名必须填写!");
                }
                return false;
          });

          form.render();

    });
</script>


{% endblock %}